<template>
  <!--授权信息  -->
  <div>
    <FormPanel ref="FormPanel" :list="initForm" :cols="2" :formType="formType" :labelWidth="180" :isSubmitBtn="false" @formChange="formChangeHandle" class="mtop" />
  </div>
</template>
<div id="allmap" style="width:0px;height:0px;"></div>
<script>
import { dictionary } from '@/utils/dictMixin';
import * as utils from '@/utils';
import * as types from '@/api/cms-api/origin/originInfo/types';

import * as bqutils from '@/utils/utils'; //工具类

import vm from './bridge';

import MP from '@/assets/js/map'; //地图

export default {
  mixins: [dictionary],
  model: {
    prop: 'pdata',
    event: 'click'
  },
  props: {
    pdata: {
      type: Object,
      default: () => ({})
    },
    formType: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      initForm: this.initFormFun(),
      map: undefined
    };
  },
  mounted() {
    //注册经销商类型变化时触发的事件
    //在审核时，给此页面的类型为只读，但是基本信息（infoBase可以编辑，会显示会触发事件，事件会触发禁用取消，覆盖整个表单的类型（表单类型为show，但是依旧可以输入，（部分）））
    if (this.formType != 'show') {
      vm.$on('changeDealerType', this.changeDealerType);
    }
  },
  methods: {
    //初始化数据（由外部调用）
    init() {
      this.initForm.forEach(x => {
        //为空不赋值
        if (this.pdata[x.fieldName]) {
          //如果是‘电池联合服务中心’则需要特殊处理一下，否则会出现类型不一致导致回显出现问题
          if (x.fieldName === 'batteryServiceStation') {
            x.initialValue = Number(this.pdata[x.fieldName]);
          } else {
            x.initialValue = this.pdata[x.fieldName];
          }
        }
      });
    },
    //表单验证通过后
    formChangeHandle(val) {
      this.pdata = Object.assign(this.pdata, val);
      //传递数据到父组件里
      this.$emit('click', this.pdata);
      //触发外部的事件，以标志着本表单已经准备完毕
      this.$emit('formchange', this.pdata);
    },
    //开放的保存方法
    saveDialog() {
      this.$refs.FormPanel.SUBMIT_FORM();
    },
    //经销商类型变更时
    changeDealerType(value) {
      //根据类型处理验证
      if (types.DEALER_INFO_DEALER_TYPE_4S === value) {
        //4S
        // vm.changeForm(this.initForm, '4S', false);
        vm.clearValidate(this.$refs.FormPanel);
      } else if (types.DEALER_INFO_DEALER_TYPE_SINGLE_AFTER_SALE === value) {
        //单一售后
        // vm.changeForm(this.initForm, 'SH', false);
        vm.clearValidate(this.$refs.FormPanel);
      } else if (types.DEALER_INFO_DEALER_TYPE_SINGLE_SALE === value) {
        //单一销售
        // vm.changeForm(this.initForm, 'XS', false);
        vm.clearValidate(this.$refs.FormPanel);
      } else if (types.DEALER_INFO_DEALER_TYPE_DIRECT_SHOP === value) {
        //SPACE
        // vm.changeForm(this.initForm, 'SPACE', false);
        vm.clearValidate(this.$refs.FormPanel);
      } else if (types.DEALER_INFO_DEALER_TYPE_NO_DIRECT_SHOP === value) {
        //RACE
        // vm.changeForm(this.initForm, 'RACE', false);
        vm.clearValidate(this.$refs.FormPanel);
      } else if (types.DEALER_INFO_DEALER_TYPE_BIG_CUSTOMER === value) {
        //大客户
        // vm.changeForm(this.initForm, 'DKH', false);
        vm.clearValidate(this.$refs.FormPanel);
      }

      //另做处理，不是简单的起用禁用
      //维修资质： 4S和单一售后必填，单一销售和SPACE RACE时灰色不填
      //SPACE和RACE的经销商级别、展厅级别、售后级别及其他售后专用字段不显示
      //SPACE，RACE
      // if(types.DEALER_INFO_DEALER_TYPE_DIRECT_SHOP === value || types.DEALER_INFO_DEALER_TYPE_NO_DIRECT_SHOP === value){
      // 	//维修资质类别
      // 	//this.initForm.find(x => x.fieldName === 'maintenanceCategory').rules;
      // 	this.initForm.find(x => x.fieldName === 'maintenanceCategory').disabled = true;
      // 	this.initForm.find(x => x.fieldName === 'maintenanceCategory').rules = [];
      // 	//销售店面级别
      // 	this.initForm.find(x => x.fieldName === 'saleLevel').hidden = true;
      // 	//售后店面级别
      // 	this.initForm.find(x => x.fieldName === 'serviceLevel').hidden = true;
      // //4S店
      // }else if(types.DEALER_INFO_DEALER_TYPE_4S === value){
      // 	this.initForm.find(x => x.fieldName === 'maintenanceCategory').disabled = false;
      // 	this.initForm.find(x => x.fieldName === 'maintenanceCategory').rules = [
      // 		{required:true,message:'必填',trigger:'blur'}
      // 	];
      // 	//销售店面级别
      // 	this.initForm.find(x => x.fieldName === 'saleLevel').hidden = false;
      // 	//售后店面级别
      // 	this.initForm.find(x => x.fieldName === 'serviceLevel').hidden = false;
      // //单一销售
      // }else if(types.DEALER_INFO_DEALER_TYPE_SINGLE_SALE === value){
      // 	this.initForm.find(x => x.fieldName === 'maintenanceCategory').disabled = true;
      // 	this.initForm.find(x => x.fieldName === 'maintenanceCategory').rules = [];
      // 	//销售店面级别
      // 	this.initForm.find(x => x.fieldName === 'saleLevel').hidden = false;
      // 	//售后店面级别
      // 	this.initForm.find(x => x.fieldName === 'serviceLevel').hidden = false;
      // //单一售后
      // }else if(types.DEALER_INFO_DEALER_TYPE_SINGLE_AFTER_SALE === value){
      // 	this.initForm.find(x => x.fieldName === 'maintenanceCategory').disabled = false;
      // 	this.initForm.find(x => x.fieldName === 'maintenanceCategory').rules = [
      // 		{required:true,message:'必填',trigger:'blur'}
      // 	];
      // 	//销售店面级别
      // 	this.initForm.find(x => x.fieldName === 'saleLevel').hidden = false;
      // 	//售后店面级别
      // 	this.initForm.find(x => x.fieldName === 'serviceLevel').hidden = false;
      // }else{
      // 	this.initForm.find(x => x.fieldName === 'maintenanceCategory').disabled = false;
      // 	this.initForm.find(x => x.fieldName === 'maintenanceCategory').rules = [];
      // 	//销售店面级别
      // 	this.initForm.find(x => x.fieldName === 'saleLevel').hidden = false;
      // 	//售后店面级别
      // 	this.initForm.find(x => x.fieldName === 'serviceLevel').hidden = false;
      // }
    },
    //销售获取经纬度
    saleAdress() {
      let self = this;
      //获取地址
      let salesAddress = this.initForm.find(x => x.fieldName === 'salesAddress').initialValue;
      if (salesAddress) {
        var city = '';
        if (salesAddress.indexOf('北京') >= 0) {
          city = '北京市';
        } else if (salesAddress.indexOf('上海') >= 0) {
          city = '上海市';
        } else if (salesAddress.indexOf('天津') >= 0) {
          city = '天津市';
        } else if (salesAddress.indexOf('重庆') >= 0) {
          city = '重庆市';
        } else if (salesAddress.indexOf('内蒙古') >= 0) {
          city = '内蒙古自治区';
        } else if (salesAddress.indexOf('宁夏') >= 0) {
          city = '宁夏回族自治区';
        } else if (salesAddress.indexOf('新疆') >= 0) {
          city = '新疆维吾尔自治区';
        } else if (salesAddress.indexOf('西藏') >= 0) {
          city = '西藏自治区';
        } else if (salesAddress.indexOf('广西') >= 0) {
          city = '广西壮族自治区';
        } else if (salesAddress.indexOf('香港') >= 0) {
          city = '香港';
        } else if (salesAddress.indexOf('澳门') >= 0) {
          city = '澳门';
        } else if (salesAddress.indexOf('省') >= 0) {
          city = salesAddress.substring(0, salesAddress.indexOf('省') + 1);
        } else {
          this.$notify.warning({ title: '提示信息', message: '请输入正确的销售运营地址!' });
          return;
        }

        MP().then(BMap => {
          var map = new BMap.Map('allmap');
          // 创建地址解析器实例
          var myGeo = new BMap.Geocoder();
          // 获取地址经纬度
          myGeo.getPoint(
            salesAddress,
            function(point, e) {
              if (e) {
                self.initForm.find(x => x.fieldName === 'salesLongitude').initialValue = point.lng;
                self.initForm.find(x => x.fieldName === 'salesLatitude').initialValue = point.lat;
              } else {
                self.$notify.warning({ title: '提示信息', message: '请输入正确的销售运营地址!' });
              }
            },
            city
          );
        });
      } else {
        this.$notify.warning({ title: '提示信息', message: '请输入销售运营地址!' });
      }
    },
    //售后获取经纬度
    buyAdress() {
      let self = this;
      //获取地址
      let serviceAddress = this.initForm.find(x => x.fieldName === 'serviceAddress').initialValue;
      if (serviceAddress) {
        var city = '';
        if (serviceAddress.indexOf('北京') >= 0) {
          city = '北京市';
        } else if (serviceAddress.indexOf('上海') >= 0) {
          city = '上海市';
        } else if (serviceAddress.indexOf('天津') >= 0) {
          city = '天津市';
        } else if (serviceAddress.indexOf('重庆') >= 0) {
          city = '重庆市';
        } else if (serviceAddress.indexOf('内蒙古') >= 0) {
          city = '内蒙古自治区';
        } else if (serviceAddress.indexOf('宁夏') >= 0) {
          city = '宁夏回族自治区';
        } else if (serviceAddress.indexOf('新疆') >= 0) {
          city = '新疆维吾尔自治区';
        } else if (serviceAddress.indexOf('西藏') >= 0) {
          city = '西藏自治区';
        } else if (serviceAddress.indexOf('广西') >= 0) {
          city = '广西壮族自治区';
        } else if (serviceAddress.indexOf('香港') >= 0) {
          city = '香港';
        } else if (serviceAddress.indexOf('澳门') >= 0) {
          city = '澳门';
        } else if (serviceAddress.indexOf('省') >= 0) {
          city = serviceAddress.substring(0, serviceAddress.indexOf('省') + 1);
        } else {
          this.$notify.warning({ title: '提示信息', message: '请输入正确的售后运营地址!' });
          return;
        }

        MP().then(BMap => {
          var map = new BMap.Map('allmap');
          // 创建地址解析器实例
          var myGeo = new BMap.Geocoder();
          // 获取地址经纬度
          myGeo.getPoint(
            serviceAddress,
            function(point, e) {
              if (e) {
                self.initForm.find(x => x.fieldName === 'serviceLongitude').initialValue = point.lng;
                self.initForm.find(x => x.fieldName === 'servicesLatitude').initialValue = point.lat;
              } else {
                self.$notify.warning({ title: '提示信息', message: '请输入正确的售后运营地址!' });
              }
            },
            city
          );
        });
      } else {
        this.$notify.warning({ title: '提示信息', message: '请输入售后运营地址!' });
      }
    },
    //搜索数据
    initFormFun() {
      let bt = { required: true, message: '必填', trigger: ['change', 'blur'] };
      let btl = [];
      let jy = { disabled: true };
      let yc = { hidden: true };
      return [
        //销售部分
        {
          type: 'DATE',
          label: '授权时间',
          fieldName: 'authTime',
          disabled: true,
          valueFormat: 'yyyy-MM-dd',
          initialValue: '',
          placeholder: '请选择',
          style: { width: '100%' },
          rules: btl,
          rules4S: btl,
          rulesSH: btl,
          rulesXS: btl,
          rulesSPACE: btl,
          rulesRACE: btl,
          rulesDKH: btl
        },
        {
          type: 'DATE',
          label: '验收通过时间',
          fieldName: 'passTime',
          valueFormat: 'yyyy-MM-dd',
          disabled: true,
          initialValue: '',
          placeholder: '请选择',
          style: { width: '100%' },
          rules: btl,
          rules4S: btl,
          rulesSH: btl,
          rulesXS: btl,
          rulesSPACE: btl,
          rulesRACE: btl,
          rulesDKH: []
        },
        {
          type: 'DATE',
          label: '入网通过时间',
          fieldName: 'joinTime',
          valueFormat: 'yyyy-MM-dd',
          disabled: true,
          initialValue: '',
          placeholder: '请选择',
          style: { width: '100%' },
          rules: btl,
          rules4S: btl,
          rulesSH: btl,
          rulesXS: btl,
          rulesSPACE: btl,
          rulesRACE: btl,
          rulesDKH: btl
        },
        {
          type: 'SELECT',
          label: '销售店面级别',
          fieldName: 'saleLevel',
          disabled: true,
          placeholder: '请选择',
          initialValue: '',
          itemList: this.createDictList('2542'),
          rules: btl,
          rules4S: btl,
          rulesSH: jy,
          rulesXS: btl,
          rulesSPACE: yc,
          rulesRACE: yc,
          rulesDKH: []
        },
        // TODO 2021-7-6 16:03:25 圣童确认 置灰解开； 下面好几项-start
        {
          type: 'INPUT_NUMBER',
          label: '展示面积',
          fieldName: 'exaibitionRoomArean',
          disabled: true,
          initialValue: undefined,
          placeholder: '请输入',
          precision: 2,
          max: 99999999.99,
          min: 0,
          rules4S: [],
          rulesSH: [],

          rulesXS: [],
          rulesSPACE: [],
          rulesRACE: [],
          rulesDKH: []
        },
        {
          type: 'INPUT_NUMBER',
          label: '办公面积',
          fieldName: 'officeArea',
          initialValue: undefined,
          disabled: true,
          placeholder: '请输入',
          precision: 2,
          max: 99999999.99,
          min: 0,
          rules4S: [],
          rulesSH: [],

          rulesXS: [],
          rulesSPACE: [],
          rulesRACE: [],
          rulesDKH: []
        },
        {
          type: 'INPUT_NUMBER',
          label: '展厅面宽',
          fieldName: 'exaibitionRoomWidth',
          disabled: true,
          initialValue: undefined,
          placeholder: '请输入',
          precision: 2,
          max: 99999999.99,
          min: 0,
          rules4S: [],
          rulesSH: [],

          rulesXS: [],
          rulesSPACE: [],
          rulesRACE: [],
          rulesDKH: []
        },
        {
          type: 'INPUT',
          label: '销售店经度',
          fieldName: 'salesLongitude',
          disabled: true,
          initialValue: '',
          placeholder: '请输入',
          maxlength: 60,
          rules4S: [],
          rulesSH: [],

          rulesXS: [],
          rulesSPACE: [],
          rulesRACE: [],
          rulesDKH: []
        },
        {
          type: 'INPUT',
          label: '销售店纬度',
          fieldName: 'salesLatitude',
          disabled: true,
          initialValue: '',
          placeholder: '请输入',
          maxlength: 60,
          rules4S: [],
          rulesSH: [],

          rulesXS: [],
          rulesSPACE: [],
          rulesRACE: [],
          rulesDKH: []
        },
        {
          type: 'INPUT',
          label: '销售运营地址',
          fieldName: 'salesAddress',
          disabled: true,
          selfCols: 2,
          rows: 1,
          initialValue: '',
          placeholder: '请输入',
          unitRender: () => {
            return (
              <el-button type="text" onClick={() => this.saleAdress()}>
                获取经纬度
              </el-button>
            );
          },
          maxlength: 240,
          rules: btl,
          rules4S: btl,
          rulesSH: [],
          rulesXS: btl,
          rulesSPACE: btl,
          rulesRACE: btl,
          rulesDKH: btl
        },

        // TODO 2021-7-6 16:03:25 圣童确认 置灰解开； 上面好几项-end

        //售后部分
        {
          type: 'INPUT_NUMBER',
          label: '工位数量',
          fieldName: 'stationNum',
          initialValue: undefined,
          disabled: true,
          placeholder: '请输入',
          hidden: false,
          precision: 0,
          max: 99999999.99,
          min: 0,
          rules4S: [],
          rulesXS: jy,
          rulesSH: [],
          rulesSPACE: [],
          rulesRACE: [],
          rulesDKH: []
        },
        {
          type: 'SELECT',
          label: '售后店面级别',
          fieldName: 'serviceLevel',
          disabled: true,
          placeholder: '请选择',
          initialValue: '',
          itemList: this.createDictList('2543'),
          rules: btl,
          rules4S: btl,
          rulesXS: jy,
          rulesSH: btl,
          rulesSPACE: yc,
          rulesRACE: yc,
          rulesDKH: []
        },

        // {type:'INPUT_NUMBER',label:'工时系数',fieldName:'laborCoefficient',initialValue:undefined,placeholder:'请输入',
        // 	precision: 2,
        // 	max:99999999.99,
        // 	min:0,
        // 	rules:[{required:true,message:'必填',trigger:'blur'}],
        // 	disabled:false
        // },

        {
          type: 'INPUT',
          label: '工时系数',
          fieldName: 'laborCoefficient',
          disabled: true,
          initialValue: '',
          maxlength: 15,
          unitRender: () => {
            return <span>%</span>;
          },
          change: val => {
            bqutils.toNumber(this.initForm, 'laborCoefficient', val);
          },
          rules: btl,
          rules4S: btl,
          rulesXS: jy,
          rulesSH: btl,
          rulesSPACE: btl,
          rulesRACE: btl,
          rulesDKH: []
        },

        {
          type: 'SELECT',
          label: '样板店',
          fieldName: 'isModelShop',
          disabled: true,
          placeholder: '请选择',
          initialValue: '',
          itemList: this.createDictList('1004'),
          rules: btl,
          rules4S: btl,
          rulesXS: jy,
          rulesSH: btl,
          rulesSPACE: btl,
          rulesRACE: btl,
          rulesDKH: []
        },
        {
          type: 'SELECT',
          label: '动力电池回收网络',
          fieldName: 'isPowerRecycing',
          placeholder: '请选择',
          disabled: true,
          initialValue: '',
          itemList: this.createDictList('1004'),
          rules: btl,
          rules4S: btl,
          rulesXS: jy,
          rulesSH: btl,
          rulesSPACE: btl,
          rulesRACE: btl,
          rulesDKH: []
        },
        {
          type: 'SELECT',
          label: '电池联合服务中心',
          fieldName: 'batteryServiceStation',
          disabled: true,
          initialValue: '',
          placeholder: '请输入',
          itemList: this.createDictList('1004'),
          maxlength: 300,
          rules: btl,
          rules4S: btl,
          rulesXS: jy,
          rulesSH: btl,
          rulesSPACE: btl,
          rulesRACE: btl,
          rulesDKH: []
        },
        {
          type: 'INPUT',
          label: '与销售店的距离',
          disabled: true,
          fieldName: 'distanceBetweenSale',
          initialValue: '',
          placeholder: '请输入',
          maxlength: 8,
          unitRender: () => {
            return <span>(M)</span>;
          },
          rules: [
            //金额验证
            { pattern: /^\d+(\.\d{1,2})?$/, message: '请填写正确的数字', trigger: 'blur' }
          ],
          rules4S: [],
          rulesXS: jy,
          rulesSH: [],
          rulesSPACE: [],
          rulesRACE: [],
          rulesDKH: []
        },
        {
          type: 'INPUT',
          label: '售后店经度',
          fieldName: 'serviceLongitude',
          disabled: true,
          initialValue: '',
          placeholder: '请输入',
          maxlength: 60,
          rules4S: [],
          rulesXS: jy,
          rulesSH: [],
          rulesSPACE: [],
          rulesRACE: [],
          rulesDKH: []
        },
        {
          type: 'INPUT',
          label: '售后店纬度',
          fieldName: 'servicesLatitude',
          disabled: true,
          initialValue: '',
          placeholder: '请输入',
          maxlength: 60,
          rules4S: [],
          rulesXS: jy,
          rulesSH: [],
          rulesSPACE: [],
          rulesRACE: [],
          rulesDKH: []
        },
        {
          type: 'INPUT_NUMBER',
          label: '车间面积',
          fieldName: 'workshopArea',
          disabled: true,
          initialValue: undefined,
          placeholder: '请输入',
          precision: 2,
          max: 99999999.99,
          min: 0,
          rules4S: [],
          rulesXS: jy,
          rulesSH: [],
          rulesSPACE: [],
          rulesRACE: [],
          rulesDKH: []
        },
        {
          type: 'INPUT',
          label: '售后运营地址',
          fieldName: 'serviceAddress',
          disabled: true,
          selfCols: 2,
          rows: 1,
          initialValue: '',
          placeholder: '请输入',
          unitRender: () => {
            return (
              <el-button type="text" onClick={() => this.buyAdress()}>
                获取经纬度
              </el-button>
            );
          },
          maxlength: 240,
          rules: btl,
          rules4S: btl,
          rulesXS: jy,
          rulesSH: btl,
          rulesSPACE: btl,
          rulesRACE: btl,
          rulesDKH: btl
        }
      ];
    }
  }
};
</script>
